// pages/nextdone/index.js

import { Note } from '../findworker/note-model.js';

var note  = new Note();

Page({

  /**
   * 页面的初始数据
   */
  data: {

    formats: {},
    readOnly: false,
    placeholder: '开始输入...',
    editorHeight: 300,
    keyboardHeight: 0,
    isIOS: false,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '自我介绍',
    })

    var that = this;

     
    
        const platform = wx.getSystemInfoSync().platform;
        const isIOS = platform === 'ios';
        this.setData({ isIOS });
        this.updatePosition(0);
        let keyboardHeight = 0;
    
        wx.onKeyboardHeightChange(res => {
          if (res.height === keyboardHeight) return
          const duration = res.height > 0 ? res.duration * 1000 : 0
          keyboardHeight = res.height
          setTimeout(() => {
            wx.pageScrollTo({
              scrollTop: 0,
              success() {
                that.updatePosition(keyboardHeight)
                that.editorCtx.scrollIntoView()
              }
            })
          }, duration)
    
        });


        that.initpage();
    
  },

  initpage:function(){

    var that = this;
    var city ;
    city =  wx.getStorageSync('city');
    var params = {city:city};

    note.getPubNoteInit((data) => {

      console.log(data);
      that.data.arealist = data.arealist;

      var noteinfo = data.noteinfo;
      if(noteinfo)
      {


        const query = wx.createSelectorQuery();

        query.select('#editor')
    
          .context(function (res) {
    
            res.context.setContents({
    
              html: noteinfo.content
    
            })

          
            that.editorCtx = res.context
            wx.pageScrollTo({
              scrollTop: 0,
              success: () => {
                that.editorCtx.scrollIntoView()
              }
            })

    
          }).exec();




         
  

        that.setData({
    
          noteinfo:data.noteinfo,
        
          }); 


          wx.hideNavigationBarLoading();
          wx.stopPullDownRefresh();

      }

   
  },params);

  },


  updatePosition(keyboardHeight) {
    const toolbarHeight = 50
    const { windowHeight, platform } = wx.getSystemInfoSync()
    let editorHeight = keyboardHeight > 0 ? (windowHeight - keyboardHeight - toolbarHeight) : windowHeight
    this.setData({ editorHeight, keyboardHeight })
  },
  calNavigationBarAndStatusBar() {
    const systemInfo = wx.getSystemInfoSync()
    const { statusBarHeight, platform } = systemInfo
    const isIOS = platform === 'ios'
    const navigationBarHeight = isIOS ? 44 : 48
    return statusBarHeight + navigationBarHeight
  },
  onEditorReady() {
    const that = this;
    console.log('fffffff');
    wx.createSelectorQuery().select('#editor').context(function (res) {
      that.editorCtx = res.context
    }).exec()
  },
  blur() {
    this.editorCtx.blur()
  },
  format(e) {
    let { name, value } = e.target.dataset
    if (!name) return
    // console.log('format', name, value)
    this.editorCtx.format(name, value)

  },
  onStatusChange(e) {
    const formats = e.detail

    console.log(formats);
    this.setData({ formats })
  },
  insertDivider() {
    this.editorCtx.insertDivider({
      success: function () {
        console.log('insert divider success')
      }
    })
  },
  clear() {
    this.editorCtx.clear({
      success: function (res) {
        console.log("clear success")
      }
    })
  },
  removeFormat() {
    this.editorCtx.removeFormat()
  },
  insertDate() {
    const date = new Date()
    const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
    this.editorCtx.insertText({
      text: formatDate
    })
  },
  insertImage() {
    const that = this
    wx.chooseImage({
      count: 1,
      success: function (res) {
        that.editorCtx.insertImage({
          src: res.tempFilePaths[0],
          data: {
            id: 'abcd',
            role: 'god'
          },
          width: '80%',
          success: function () {
            console.log('insert image success')
          }
        })
      }
    })
  },




  savepubinfo: function (e) {
    var that = this;

    var content = '';


    
    that.editorCtx.getContents({

      success: (res) => {

        console.log(res.html);

        content = res.html;



        if (content == "") {
          wx.showModal({
            title: '提示',
            content: '请输入个人介绍',
            showCancel: false
          })
          return
        }

        var params = {
          content:content
        
        };

        note.Savecontent((data) => {
         
      wx.redirectTo({
        url: '/pages/user/index',
      })
          
      },params);



      }})


    


    },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function(){
    wx.showNavigationBarLoading();
    this.initpage();
  },

  //分享效果
  onShareAppMessage: function () {
    var that = this;
      return {
          title:'自我介绍' ,
          path: '/pages/index/index'
      }
  }
})